package com.ruiysoft.service.order.mq;

import com.alibaba.fastjson.JSON;
import com.ruiysoft.conf.mq.OrderMqConfig;
import com.ruiysoft.service.order.IOrderWechatService;
import com.ruiysoft.service.order.impl.OrderMonitorImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.ruiysoft.conf.mq.OrderMqConfig.*;

@Component
public class OrderMonitorSubscribe {

    private Logger logger = LoggerFactory.getLogger(OrderMonitorSubscribe.class);

    @Autowired
    IOrderWechatService orderWechatService;

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(value = OrderMqConfig.immediateCancelQueueName,
                                    durable = "true",
                                    autoDelete = "true"),
                            exchange = @Exchange(value = immediateCancelExchangeName, autoDelete = "false"),
                            key = immediateCancelRouteKey
                    )

            }
    )
    @Transactional
    public void onMessageCancelOrderAfterMinute30(Message message) {
	    String strOMM = new String(message.getBody(), StandardCharsets.UTF_8);
	    OrderMonitorImpl.OrderMonitorMessage orderMonitorMessage = JSON.parseObject(strOMM,OrderMonitorImpl.OrderMonitorMessage.class);
	    String orderNo = orderMonitorMessage.getOrderNo();
	    //TODO:回调订单接口-取消订单
        orderWechatService.orderCancel(orderNo,"支付超时自动取消");
    }

    /**
     * 延时队列定义
     */
    @Bean(name = "orderSave30MinuteQueue")
    public org.springframework.amqp.core.Queue delayQueue() {
        Map<String, Object> params = new HashMap<>();
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange", immediateCancelExchangeName);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
        params.put("x-dead-letter-routing-key", immediateCancelRouteKey);
        return new org.springframework.amqp.core.Queue(delay30MinuteQueueName, true, false, false, params);
    }


    @Bean(name = "orderSave30MinuteExchange")
    public DirectExchange deadLetterExchange() {
        // 一共有三种构造方法，可以只传exchange的名字， 第二种，可以传exchange名字，是否支持持久化，是否可以自动删除，
        //第三种在第二种参数上可以增加Map，Map中可以存放自定义exchange中的参数
        return new DirectExchange(delay30MinuteExchangeName, true, false);
    }

    @Bean(name = "orderSave30MinuteBinding")
    //把延时消费的队列和立即消费的exchange绑定在一起
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(deadLetterExchange()).with(delay30MinuteRouteKey);
    }


}
